var pomelo = require('pomelo');
var logger = require('pomelo-logger').getLogger(__filename);
/**
 * Init app for client.
 */
var app = pomelo.createApp();
app.set('name', 'HelloWorld');

// configure for global
app.configure('production|development', function() {
  app.loadConfig('mysql', app.getBase() + '/../game-server/config/mysql.json');
  
});

// app configuration
app.configure('production|development', 'connector', function(){
  app.set('connectorConfig',
    {
      connector : pomelo.connectors.hybridconnector,
      heartbeat : 3,
      useDict : true,
      useProtobuf : false
    });
});
app.configure('production|development', 'master', function() {
     //app.enable('systemMonitor');
});

app.configure('production|development', 'gate', function(){
	app.set('connectorConfig',
		{
			connector : pomelo.connectors.hybridconnector,
			useProtobuf : false
		});
});

// Configure database
app.configure('production|development', 'login|area|connector|master', function() {
  var dbclient = require('./app/dao/mysql/mysql').init(app);
  app.set('dbclient', dbclient);
  //app.load(pomelo.sync, {path:__dirname + '/app/dao/mapping', dbclient: dbclient});
});

var loginServerRouter = function(session, msg, app, cb) {
  logger.info("loginServerRouter","loginServerRouter = ",session," msg = ",msg);
  var loginServers = app.getServersByType('login');
  var id = loginServers[Math.round(Math.random()*10000) % loginServers.length].id;
  cb(null, id);
}
app.route('login', loginServerRouter);

app.enable('systemMonitor');

// start app
app.start();

process.on('uncaughtException', function (err) {
  console.error(' Caught exception: ' + err.stack);
});
